package com.takeoff.iot.modbus.test.listener;

import com.takeoff.iot.modbus.common.message.ModbusMessage;
import com.takeoff.iot.modbus.netty.channel.ModbusChannel;
import com.takeoff.iot.modbus.netty.listener.ModbusListener;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
// 将数据写入mq队列
public class WriteMqListener implements ModbusListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void receive(ModbusChannel channel, ModbusMessage message) {
        try{
            rabbitTemplate.convertAndSend("rawDataQueues","", Hex.toHexString(message.data().toBytes()));
        } catch (AmqpException e) {
            log.debug("AmqpException",e);
        }
    }
}
